package com.test.system.admin.controller;

import com.test.system.admin.pojo.Menu;
import com.test.system.admin.pojo.TableData;
import com.test.system.admin.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 徒有琴
 */
@Controller
@RequestMapping("menu.html")
public class MenuController {
    @Autowired
    private MenuService menuService;

    @RequestMapping
    public String page() {
        return "menu";
    }

    @RequestMapping(params = "act=table")
    @ResponseBody
    public TableData table() {
        TableData tableData = new TableData();
        List<Menu> menuList = menuService.getMenuList();
        tableData.setData(menuList);
        tableData.setCount(menuList.size());
        return tableData;
    }

    @RequestMapping(params = "act=tree")
    @ResponseBody
    public List<Menu> menuTree() {
        return menuService.getMenuTree(null);
    }

    @RequestMapping(params = "act=add")
    @ResponseBody
    public Map<String, Object> add(Menu menu) {
        Map<String, Object> result = new HashMap<>();
        try {
            menuService.insert(menu);
            result.put("status", true);
        } catch (Exception e) {
            e.printStackTrace();
            result.put("status", false);
            result.put("message", e.getMessage());
        }
        return result;
    }
    @RequestMapping(params = "act=update")
    @ResponseBody
    public Map<String, Object> update(Menu menu) {
        Map<String, Object> result = new HashMap<>();
        try {
            menuService.update(menu);
            result.put("status", true);
        } catch (Exception e) {
            e.printStackTrace();
            result.put("status", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

}
